﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO_DTO_WS.DAO;
using DAO_DTO_WS.DTO;

namespace testPre
{
    public partial class ChiTietSanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                string maSanPham = Request.QueryString["masanpham"];
                if (maSanPham.Equals(null))
                {
                    Response.Redirect("DanhSachSanPham.aspx");
                }
                else
                {
                    SanPhamDTO sp = new SanPhamDTO();
                    sp = SanPhamDAO.laySPTheoMa(int.Parse(maSanPham));
                    lbTenSanPham.Text = sp.ThongTin;
                    lbGiaTruoc.Text = sp.GiaGoc.ToString();
                    lbGiaSau.Text = sp.GiaTietKiem.ToString();
                    lbDiemNoiBat.Text = sp.DiemNoiBat;
                    lbThoiGianBatDau.Text = sp.ThoiGianBatDau.ToString();
                    lbThoiGianKetThuc.Text = sp.ThoiGianKetThuc.ToString();

                    List<LoiBinhDTO> dsLoiBinh = LoiBinhDAO.LayCommentTheoSp(sp.MaSanPham);
                    int soLuong = dsLoiBinh.Count;
                    for (int i = 0; i < soLuong; ++i)
                    {
                        Response.Write("<div>" + dsLoiBinh[i].NoiDung + "</div>");
                    }
                }
            }
        }

        protected void btnComment_Click(object sender, EventArgs e)
        {
            if (string.Compare(TextBox1.Text,string.Empty,false)==0)
            {
                lbComment.Text = "Vui lòng viết bình luận vào ô bên dưới";
            }
           
            else
            {
                string maSanPham = Request.QueryString["masanpham"];
                try
                {
                    int idsanpham = int.Parse(maSanPham);
                    LoiBinhDAO.ThemLoiBinh(5, idsanpham, TextBox1.Text);
                }
                catch (Exception ex)
                {
                    Response.Redirect("ChiTietSanPham.aspx" + "?masanpham=" + maSanPham);
                }
            }
        }
    }
}
